package cn.cdeden.knowledge.domain.dto;

import cn.cdeden.common.translation.annotation.Translation;
import cn.cdeden.common.translation.constant.TransConstant;
import cn.cdeden.knowledge.config.OnlineConfig;

import cn.cdeden.knowledge.domain.TreeDownloadFiles;
import io.github.linpeilie.annotations.AutoMapper;
import io.github.linpeilie.annotations.AutoMappers;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;

import java.io.Serializable;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @ProjectName : ecm_hou
 * @Package : com.cdyd.ecm.modules
 * @ClassName : Tree
 * @Author : Yu.
 * @Description : // TODO
 * @Date : 2022/10/26 11:23
 * @Version : 1.0
 */
@Data
@AllArgsConstructor
@NoArgsConstructor
@AutoMappers({@AutoMapper(target = TreeDownloadFiles.class)})
public class FilesTree implements Serializable {

    private static final long serialVersionUID = 1L;

//    @ApiModelProperty(value = "文件id")
    private Long id;


//    @ApiModelProperty(value = "企业_文件编号")
    private Long fileId;

//    @ApiModelProperty(value = "团队编号")
    private String teamId;

//    @ApiModelProperty(value = "团队名称")
@Translation(type = TransConstant.USER_ID_TO_NICKNAME,mapper = "createId")
    private String teamName;

//    @ApiModelProperty(value = "上级的编号,暂时 在个人文档中有这字段")
    private String superiorsIds;

//    @ApiModelProperty(value = "源文件树编号")
    private Long fileTreeId;

//    @ApiModelProperty(value = "源文件树类型")
    private String fileTreeType;

//    @ApiModelProperty(value = "父亲节点编号")
    private String parentId;

//    @ApiModelProperty(value = "名称")
    private String realName;

//    @ApiModelProperty(value = "后缀名")
    private String suffix;

//    @ApiModelProperty(value = "上级路径")
    private String superiorsPath;

//    @ApiModelProperty(value = "路径")
    private String path;

//    @ApiModelProperty(value = "类型")
    private String type;

//    @ApiModelProperty(value = "储存表id")
    private String storageId;

//    @ApiModelProperty(value = "储存服务器类型")
    private String storageType;

//    @ApiModelProperty(value = "文件大小")
    private Long size;

//    @ApiModelProperty(value = "库的类型")
    private String  libraryType;

//    @ApiModelProperty(value = "版本号")
    private String edition;

//    @ApiModelProperty(value = "版本编码")
    private String editionName;

//    @ApiModelProperty(value = "标签记录id")
    private String LabelId ;

//    @ApiModelProperty(value = "收藏")
    private String collection;

//    @ApiModelProperty(value = "关注")
    private String attention;


//    @ApiModelProperty(value = "文件缩略图")
//    @Translation(type = TransConstant.MINIO_ID_TO_THUMBNAIL_URL , mapper = "storageId")
    private String thumbnailUrl;

//    @ApiModelProperty(value = "创建编号")
    private String createId;

//    @ApiModelProperty(value = "创建者")
    private String createBy;

//    @ApiModelProperty(value = "创建日期")
    private Date createTime;

    @Translation(type = cn.cdeden.common.translation.constant.TransConstant.USER_ID_TO_NICKNAME)
    private String creator;
    @Translation(type = cn.cdeden.common.translation.constant.TransConstant.USER_ID_TO_NICKNAME)
    private String creatorName;

//    @ApiModelProperty(value = "更新编号")
    private String updateId;

//    @ApiModelProperty(value = "更新者")
    private String updateBy;


//    @ApiModelProperty(value = "更新时间")
    private Date updateTime;

//    @ApiModelProperty(value = "是否预览")
    private Boolean  read = true;

//    @ApiModelProperty(value = "是否编辑")
    private Boolean edit = true;

//    @ApiModelProperty(value = "是否下载")
    private Boolean download = true;

//    @ApiModelProperty(value = "是否打印")
    private Boolean print = true;

//    @ApiModelProperty(value = "文件的MD5")
    private String fileMd5;

//    @ApiModelProperty(value = "文件编辑类型")
    private String documentType;

//    @ApiModelProperty(value = "文件编辑类型")
    private String preview ;

//    @ApiModelProperty(value = "备注")
    private String remarks;

//    @ApiModelProperty(value = "标签编号")
    private String labelIds;

//    @ApiModelProperty(value = "标签名称")
    private String labelNames;


//    @ApiModelProperty(value = "级别标签编号")
    private String levelLabelIds;

//    @ApiModelProperty(value = "级别标签名称")
    private String levelLabelNames;

//    @ApiModelProperty(value = "权限")
    private String perm;

//    @ApiModelProperty(value = "水印文字")
    private String  watermarkTxt;

    //预览服务地址
    private OnlineConfig previewAddress;

//    @ApiModelProperty(value = "儿子数据")
    private List<FilesTree> children = new ArrayList<>();

    public Boolean setEditIf(Boolean edit) {
        if(!edit) this.edit = edit;
        return edit;
    }

    /**
     * 将FilesTree列表根据其parentId属性进行分组，并筛选出指定ids列表中的对象。
     * 同时设置每个FilesTree对象的libraryType属性。
     *
     * @param filesTrees 文件树列表
     * @param ids        需要筛选的ID列表
     * @param libraryType 图书馆类型
     * @return 筛选后的文件树列表
     */
    public static  List<FilesTree> listToTerr(List<FilesTree> filesTrees , List<String> ids, String libraryType){
        if (CollectionUtils.isEmpty(filesTrees) || CollectionUtils.isEmpty(ids)){
            return  new ArrayList<>();
        }

        filesTrees.forEach(tree -> tree.setLibraryType(libraryType)); //设置库的类型
        Map<String, List<FilesTree>> collect = filesTrees.stream().collect(Collectors.groupingBy(FilesTree::getParentId));
        for (FilesTree filesTree : filesTrees) {
            String of = String.valueOf(filesTree.getId());
            List<FilesTree> files = collect.get(of);
            if (CollectionUtils.isNotEmpty(files)) {
                files.forEach(tree -> {
                    if (tree.getChildren() == null) {
                        tree.setChildren(new ArrayList<>());
                    }
                } );
            }

            filesTree.setChildren(files);
        }

        return filesTrees.stream().filter(downloadFiles -> ids.contains(downloadFiles.getId().toString())).collect(Collectors.toList());
    }

    //根据id 去找到树节点是否有这个儿子有则返回数据
    public FilesTree folderSonFiles(String id) {
        FilesTree tree = null;
        if (CollectionUtils.isNotEmpty(this.getChildren())) {
            Stack<FilesTree> stack = new Stack<>();
            while (!stack.isEmpty()) {
                FilesTree pop = stack.pop();
                if (StringUtils.equals(String.valueOf(pop.getId()), id)) {
                    return pop;
                }
                List<FilesTree> trees = pop.children;
                if (CollectionUtils.isNotEmpty(trees)) {
                    for (FilesTree filesTree : trees) {
                        stack.push(filesTree);
                    }
                }

            }
        }
        return null;
    }





}
